

package com.viweb.storemanager.core;

import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;

public class ObjectListModel extends AbstractListModel {

    List<?> listData;
    Object selectedObject;

    /** Creates a new instance of DefaultObjectListModel */
    public ObjectListModel() {
        this.listData = new ArrayList();

        if (getSize() > 0) {
            this.selectedObject = getElementAt(0);
        }
    }

    /** Creates a new instance of DefaultObjectListModel */
    public ObjectListModel(List<?> listData) {
        if (listData == null) {
            this.listData = new ArrayList();
        } else {
            this.listData = listData;
        }

        if (getSize() > 0) {
            this.selectedObject = getElementAt(0);
        }
    }

    public int getSize() {
        return this.listData.size();
    }

    public Object getElementAt(int index) {
        if (index >= 0 && index < listData.size()) {
            return listData.get(index);
        } else {
            return null;
        }
    }

    public void setSelectedItem(Object anItem) {
        if ((this.selectedObject != null && !this.selectedObject.equals(anItem))
                || this.selectedObject == null && anItem != null) {
            this.selectedObject = anItem;
            fireContentsChanged(this, -1, -1);
        }
    }

    public int getIndexOf(Object obj) {
        return this.listData.indexOf(obj);
    }

    public Object getSelectedItem() {
        return this.selectedObject;
    }

    public void updateModel(List listData) {
        if (listData == null) {
            this.listData = new ArrayList();
        } else {
            this.listData = listData;
        }
    }
}
